﻿using Hotel_Project.Bean;
using Hotel_Project.Tools.MySqlData_Helps;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Hotel_Project.BIZ
{
    internal class GuestInfoBIZ
    {

        /// <summary>
        /// 将客人信息保存到数据库
        /// </summary>
        /// <param name="guest"></param>
        /// <returns></returns>
        public int Save(GuestInfo guest)
        {
            //bool mark = true;
            //DataTable dt = new DataTable();
            //string selectSql = "select g_Name,Id_card,Telephone from guestinfo";
            //MySQLHelper.PrepareMySql(selectSql);
            //dt = MySQLHelper.ExecuteQuery();
            //for (int i = 0; i < dt.Rows.Count; i++)
            //{
            //    if (dt.Rows[i]["g_Name"].ToString().Equals(guest.Name) || dt.Rows[i]["Id_card"].ToString().Equals(guest.IDCard) || dt.Rows[i]["Telephone"].ToString().Equals(guest.Phone))
            //    {
            //        mark = false;
            //        string updateSql = "update guestinfo set g_Name=@g_Name,Sex=@Sex,Address=@Address,Telephone=@Telephone,Remark=@Remark where Id_card=@Id_card";
            //        MySQLHelper.PrepareMySql(updateSql);
            //        MySQLHelper.SetParameters("g_Name", guest.Name);
            //        MySQLHelper.SetParameters("Sex", guest.Sex);
            //        MySQLHelper.SetParameters("Address", guest.Address);
            //        MySQLHelper.SetParameters("Telephone", guest.Phone);
            //        MySQLHelper.SetParameters("Remark", guest.Remark);
            //        MessageBox.Show("保存成功！！");
            //        return MySQLHelper.ExecuteNonQuery();
            //    }
            //}
            //if (mark)
            //{
            string sql = "insert into guestinfo (g_Name,Sex,Id_card,Address,Telephone,Remark,room_number,g_datetime) values " +
            "(@g_Name,@Sex,@Id_card,@Address,@Telephone,@Remark,@room_number,@g_datetime)";
            MySQLHelper.PrepareMySql(sql);
            MySQLHelper.SetParameters("g_Name", guest.Name);
            MySQLHelper.SetParameters("Sex", guest.Sex);
            MySQLHelper.SetParameters("Id_card", guest.IDCard);
            MySQLHelper.SetParameters("Address", guest.Address);
            MySQLHelper.SetParameters("Telephone", guest.Phone);
            MySQLHelper.SetParameters("Remark", guest.Remark);
            MySQLHelper.SetParameters("room_number", guest.Room_number);
            MySQLHelper.SetParameters("g_datetime", guest.Date_Time);
            return MySQLHelper.ExecuteNonQuery();
            //}
            //return -1;

        }
        /// <summary>
        /// 对客人的联系方式进行删除操作
        /// </summary>
        /// <param name="name"></param>
        /// <param name="phone"></param>
        /// <returns></returns>
        public int Delete(string name, int phone)
        {
            string sql = "delete from guestinfo where g_Name=@g_Name and Telephone=@Telephone";
            MySQLHelper.PrepareMySql(sql);
            MySQLHelper.SetParameters("g_Name", name);
            MySQLHelper.SetParameters("Telephone", phone);
            return MySQLHelper.ExecuteNonQuery();
        }
        /// <summary>
        /// 输入客人姓名查找这个老客人的信息
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public List<GuestInfo> Search(string name)
        {
            DataTable dt = new DataTable();
            string sql = "select g_Name,Sex,Id_card,Address,Telephone,Remark from guestinfo where g_Name=@g_Name";
            MySQLHelper.PrepareMySql(sql);
            MySQLHelper.SetParameters("g_Name", name);
            dt = MySQLHelper.ExecuteQuery();
            List<GuestInfo> list = new List<GuestInfo>();
            if (dt.Rows.Count > 0)
            {
                GuestInfo guest = new GuestInfo();
                guest.Name = dt.Rows[0]["g_Name"].ToString();
                guest.Sex = dt.Rows[0]["Sex"].ToString();
                guest.IDCard = dt.Rows[0]["Id_card"].ToString();
                guest.Address = dt.Rows[0]["Address"].ToString();
                guest.Phone = dt.Rows[0]["Telephone"].ToString();
                guest.Remark = dt.Rows[0]["Remark"].ToString();
                list.Add(guest);
            }
            return list;
        }
    }
}
